package com.example.zhunongzonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhunongzonghe.beans.ZnFarmSpecificationBean;
import com.example.zhunongzonghe.beans.ZnFarmproduceBean;
import com.example.zhunongzonghe.dao.ZnFarmproduceMapper;
import com.example.zhunongzonghe.entity.ZnFarmSpecification;
import com.example.zhunongzonghe.entity.ZnFarmproduce;
import com.example.zhunongzonghe.entity.ZnSort;
import com.example.zhunongzonghe.entity.ZnUsers;
import com.example.zhunongzonghe.service.IZnFarmproduceService;
import com.example.zhunongzonghe.service.IZnUsersService;
import com.example.zhunongzonghe.service.ZnCollectFarmService;
import com.example.zhunongzonghe.service.ZnFarmSpecificationService;
import com.example.zhunongzonghe.utils.BeanCopyUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ZnFarmproduceServiceImpl extends ServiceImpl<ZnFarmproduceMapper, ZnFarmproduce> implements IZnFarmproduceService {

    @Autowired
    ZnFarmproduceMapper znFarmproduceMapper;

    @Resource
    private ZnFarmSpecificationService specificationService;
    @Resource
    private ZnCollectFarmService znCollectFarmService;
    @Resource
    private IZnUsersService znUsersService;

    /**
     * mybatis-plus-join查询全部农产品数据
     * @return
     */
    @Override
    public Page<ZnFarmproduceBean> SearchFarmAllInfo(Integer pageId, Integer pageNum, Object Search) {
        Page<ZnFarmproduceBean> page = new Page<>(pageId,pageNum);
        MPJLambdaWrapper<ZnFarmproduce> wrapper = new MPJLambdaWrapper<ZnFarmproduce>()
                .selectAll(ZnFarmproduce.class)
                .select(ZnSort::getSortContent)
                .like(ZnSort::getSortContent,Search)
                .or().like(ZnFarmproduce::getFarmName,Search)
                .or().like(ZnFarmproduce::getFarmId,Search)
                .or().like(ZnFarmproduce::getFarmIntro,Search)
                .leftJoin(ZnSort.class,ZnSort::getSortId,ZnFarmproduce::getSortId);
        Page<ZnFarmproduceBean> beanPage =znFarmproduceMapper.selectJoinPage(page,ZnFarmproduceBean.class,wrapper);
        for (ZnFarmproduceBean znFarmBean: beanPage.getRecords()
        ) {
            LambdaQueryWrapper<ZnFarmSpecification> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZnFarmSpecification::getZnFarmId,znFarmBean.getFarmId());
            List<ZnFarmSpecification> znFarmSpecifications = specificationService.list(queryWrapper);
            List<ZnFarmSpecificationBean> specBean =
                    BeanCopyUtils.copyBeanList(znFarmSpecifications,ZnFarmSpecificationBean.class);
            znFarmBean.setSpecificationBeans(specBean);
        }
        return beanPage;
    }
    /**
     * mybatis-plus-join查询全部已上架农产品数据
     * @return
     */
    @Override
    public Page<ZnFarmproduceBean> FindAllProduceInfo(Integer pageId, Integer pageNum, Object Search) {
        Page<ZnFarmproduceBean> page = new Page<>(pageId,pageNum);
        MPJLambdaWrapper<ZnFarmproduce> wrapper = new MPJLambdaWrapper<ZnFarmproduce>()
                .selectAll(ZnFarmproduce.class)
                .select(ZnSort::getSortContent)
                .and(qw -> qw.like(ZnSort::getSortContent, Search)
                        .or().like(ZnFarmproduce::getFarmName, Search)
                        .or().like(ZnFarmproduce::getFarmId, Search)
                        .or().like(ZnFarmproduce::getFarmIntro, Search))
                .leftJoin(ZnSort.class, ZnSort::getSortId, ZnFarmproduce::getSortId)
                .eq(ZnFarmproduce::getStatus, 1);
        Page<ZnFarmproduceBean> beanPage =znFarmproduceMapper.selectJoinPage(page,ZnFarmproduceBean.class,wrapper);
        for (ZnFarmproduceBean znFarmBean: beanPage.getRecords()
        ) {
            LambdaQueryWrapper<ZnFarmSpecification> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZnFarmSpecification::getZnFarmId,znFarmBean.getFarmId());
            List<ZnFarmSpecification> znFarmSpecifications = specificationService.list(queryWrapper);
            List<ZnFarmSpecificationBean> specBean =
                    BeanCopyUtils.copyBeanList(znFarmSpecifications,ZnFarmSpecificationBean.class);
            znFarmBean.setSpecificationBeans(specBean);
        }
        return beanPage;
    }

    @Override
    public ZnFarmproduceBean DisInfoFarm(Integer farmId,String username) {
        ZnUsers znUsers = znUsersService.getUserName(username);
        MPJLambdaWrapper<ZnFarmproduce> wrapper = new MPJLambdaWrapper<ZnFarmproduce>()
                .selectAll(ZnFarmproduce.class)
                .select(ZnSort::getSortContent)
                .eq(ZnFarmproduce::getFarmId,farmId)
                .leftJoin(ZnSort.class,ZnSort::getSortId,ZnFarmproduce::getSortId);
        ZnFarmproduceBean znFarmhouseBean = znFarmproduceMapper.selectJoinOne(ZnFarmproduceBean.class,wrapper);
        LambdaQueryWrapper<ZnFarmSpecification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmSpecification::getZnFarmId,farmId);
        List<ZnFarmSpecification> specifications = specificationService.list(queryWrapper);
        List<ZnFarmSpecificationBean> beanList =
                BeanCopyUtils.copyBeanList(specifications,ZnFarmSpecificationBean.class);
        znFarmhouseBean.setSpecificationBeans(beanList);
        znFarmhouseBean.setCollectOrNot(znCollectFarmService.isCollect(farmId,znUsers.getId()));
        znFarmhouseBean.setCollectCount(znCollectFarmService.CollectCounts(farmId));
        return znFarmhouseBean;
    }

    @Override
    public ZnFarmproduceBean NotLogInInfoFarm(Integer farmId) {
        MPJLambdaWrapper<ZnFarmproduce> wrapper = new MPJLambdaWrapper<ZnFarmproduce>()
                .selectAll(ZnFarmproduce.class)
                .select(ZnSort::getSortContent)
                .eq(ZnFarmproduce::getFarmId,farmId)
                .leftJoin(ZnSort.class,ZnSort::getSortId,ZnFarmproduce::getSortId);
        ZnFarmproduceBean znFarmhouseBean = znFarmproduceMapper.selectJoinOne(ZnFarmproduceBean.class,wrapper);
        LambdaQueryWrapper<ZnFarmSpecification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmSpecification::getZnFarmId,farmId);
        List<ZnFarmSpecification> specifications = specificationService.list(queryWrapper);
        List<ZnFarmSpecificationBean> beanList =
                BeanCopyUtils.copyBeanList(specifications,ZnFarmSpecificationBean.class);
        znFarmhouseBean.setSpecificationBeans(beanList);
        znFarmhouseBean.setCollectCount(znCollectFarmService.CollectCounts(farmId));
        return znFarmhouseBean;
    }

    /**
     * 通过分类显示农产品
     * @param pageId
     * @param pageNum
     * @param sort
     * @return
     */
    @Override
    public Page<ZnFarmproduceBean> SortSearchFarm(Integer pageId, Integer pageNum, String sort) {
        Page<ZnFarmproduceBean> page = new Page<>(pageId,pageNum);
        MPJLambdaWrapper<ZnFarmproduce> wrapper = new MPJLambdaWrapper<ZnFarmproduce>()
                .selectAll(ZnFarmproduce.class)
                .select(ZnSort::getSortContent)
                .eq(ZnSort::getSortContent,sort)
                .leftJoin(ZnSort.class,ZnSort::getSortId,ZnFarmproduce::getSortId)
                .eq(ZnFarmproduce::getStatus,1);
        Page<ZnFarmproduceBean> znFarmBeanPage = znFarmproduceMapper.selectJoinPage(page,ZnFarmproduceBean.class,wrapper);
        for (ZnFarmproduceBean znFarmBean: znFarmBeanPage.getRecords()
        ) {
            LambdaQueryWrapper<ZnFarmSpecification> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZnFarmSpecification::getZnFarmId,znFarmBean.getFarmId());
            List<ZnFarmSpecification> znFarmSpecifications = specificationService.list(queryWrapper);
            List<ZnFarmSpecificationBean> specBean =
                    BeanCopyUtils.copyBeanList(znFarmSpecifications,ZnFarmSpecificationBean.class);
            znFarmBean.setSpecificationBeans(specBean);
        }
        return znFarmBeanPage;
    }

    @Override
    public List<ZnFarmproduceBean> HomeFarmAll() {
        List<ZnFarmproduce> znFarmproduceList = znFarmproduceMapper.getRandomProducts();
        List<ZnFarmproduceBean> beanList = BeanCopyUtils.copyBeanList(znFarmproduceList,ZnFarmproduceBean.class);
        for (ZnFarmproduceBean znFarmBean: beanList
        ) {
            LambdaQueryWrapper<ZnFarmSpecification> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZnFarmSpecification::getZnFarmId,znFarmBean.getFarmId());
            List<ZnFarmSpecification> znFarmSpecifications = specificationService.list(queryWrapper);
            List<ZnFarmSpecificationBean> specBean =
                    BeanCopyUtils.copyBeanList(znFarmSpecifications,ZnFarmSpecificationBean.class);
            znFarmBean.setSpecificationBeans(specBean);
        }
        return beanList;
    }

    @Override
    public List<ZnFarmproduceBean> HomeProduceTop() {
        List<ZnFarmproduce> list = znFarmproduceMapper.getTopProducts();
        List<ZnFarmproduceBean> beanList = BeanCopyUtils.copyBeanList(list,ZnFarmproduceBean.class);
        return beanList;
    }
}
